import { Action, ActionKey } from "@/action/Action";
import { localFile } from "@/localFile/LocalFile";
import { projectService } from "../../service";
import { GraphNode } from "../../project/GraphNode";
import { loading } from "@/common/Loading";

export class ImportReqifAction extends Action {
  keyName=ActionKey.importReqif

  async run(modelId: string) {
    const project = app.activeProject;
    if (!project) throw new Error("当前无打开项目！");
    await app.$confirm
      .popup({
        title: '提示',
        subTitle: '导入Reqif',
        content: '系统只读取ReqIF.ID,ReqIF.Name,ReqIF.Text字段。导入后将生成新的需求表和需求存放在独立的包内。',
        okText: '确定',
        status: 'warn',
        cancelText: '',
        centerBtnText: '打开模板',
        onClickCenter: () => localFile.openFile('/files/需求表导入模版.reqif')
      })
      .then(async () => {
        const res = await localFile.chooseFile({
          filters: [{
            name: 'Reqif Files',
            extensions: ['reqif', 'reqifz']
          }]
        });
        if (res.canceled) return;
        const filePath = res.files[0]?.path;
        if (!filePath) return;
        loading.popup("正在reqif文件...");
        projectService.importReqif({
          projectId: project.id,
          filePath,
          parentId: modelId
        }).finally(() => loading.close());
      });
  }

  onSysMenuClick() {
    const project = app.activeProject;
    if (!project) throw new Error("当前无打开项目！");
    this.run(project.nodePool.rootNodes[0].id);
  }

  onTreeMenuClick(graphNode: GraphNode) {
    if (!graphNode) return;
    this.run(graphNode.id);
  }

}

export class ImportExcelAction extends Action {
  keyName=ActionKey.importExcel
  async onApiCall(params: { [k: string]: any; }): Promise<any> {
    let { filePath } = params;
    const project = app.activeProject;
    const modelId = app.activeProject?.nodePool?.rootNodes?.[0]?.id;
    if (!project) throw new Error("当前无打开项目！");
    if (!modelId) throw new Error("当前项目无根节点");
    if (!filePath) {
      const res = await localFile.chooseFile({
        filters: [{
          name: 'Excel Files',
          extensions: ['xls', 'xlsx']
        }]
      });
      if (res.canceled) throw new Error('操作已取消');
      filePath = res.files[0]?.path;
      if (!filePath) throw new Error('未选择文件');
    }

    loading.popup("正在导入...");
    await projectService.importExcel({
      projectId: project.id,
      filePath,
      parentId: modelId
    }).catch(err => {
      throw new Error(err.message);
    }).finally(() => loading.close());
    return filePath;
  }
  async run(modelId: string) {
    const project = app.activeProject;
    if (!project) throw new Error("当前无打开项目！");
    app.$confirm
      .popup({
        title: '提示',
        subTitle: '导入Excel',
        content: '系统只读取Excel文件的id，name,text字段。导入后将生成新的需求表和需求存放在独立的包内。',
        okText: '确定',
        status: 'warn',
        cancelText: '',
        centerBtnText: '打开模板',
        onClickCenter: async () => localFile.renameAndOpen('/files/需求表导入模版.bak', 'xlsx')
      })
      .then(async () => {
        const res = await localFile.chooseFile({
          filters: [{
            name: 'Excel Files',
            extensions: ['xls', 'xlsx']
          }]
        });
        if (res.canceled) return;
        const filePath = res.files[0]?.path;
        if (!filePath) return;
        loading.popup("正在导入...");
        projectService.importExcel({
          projectId: project.id,
          filePath,
          parentId: modelId
        }).finally(() => loading.close());
      });
  }

  onSysMenuClick() {
    const project = app.activeProject;
    if (!project) throw new Error("当前无打开项目！");
    this.run(project.nodePool.rootNodes[0].id);
  }

  onTreeMenuClick(graphNode: GraphNode) {
    if (!graphNode) return;
    this.run(graphNode.id);
  }

}
